package com.elong.qadev.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.elong.qadev.entity.User;
import com.elong.qadev.entity.WebuiCase;
import com.elong.qadev.run.RunWebui;
import com.elong.qadev.service.WebuiCaseServiceI;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("all")
public class WebuiCaseAction extends ActionSupport {
	Logger logger = Logger.getLogger(WebuiCaseAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private WebuiCaseServiceI webuiCaseService;

	private String caseId;
	private WebuiCase wc;
	private String driverStr;

	public String getDriverStr() {
		return driverStr;
	}

	public void setDriverStr(String driverStr) {
		this.driverStr = driverStr;
	}

	public String getCaseId() {
		return caseId;
	}

	public void setCaseId(String caseId) {
		this.caseId = caseId;
	}

	public WebuiCase getWc() {
		return wc;
	}

	public void setWc(WebuiCase wc) {
		this.wc = wc;
	}

	@Action(value = "webuiCaseSave")
	public void webuiCaseSave() {
		WebuiCase wcOld = webuiCaseService.getWebuiCase(Integer
				.parseInt(caseId));
		if (wcOld != null) {
			Date oldDate = wcOld.getCreateDateTime();
			boolean isDelete = webuiCaseService.deleteWebuiCaseByCaseId(Integer
					.parseInt(caseId));
			if (isDelete)
				logger.info("保存前，用例数据删除成功，提取创建时间：" + oldDate);
			wc.setCreateDateTime(oldDate);
			wc.setOwner(wcOld.getOwner());
		} else {
			wc.setCreateDateTime(new Date());
			User u = (User) ActionContext.getContext().getSession().get("user");
			wc.setOwner(u.getName());
		}

		if (wc != null) {
			wc.setCaseId(Integer.parseInt(caseId));
			wc.setCaseType("FLIGHT/ORDER");
			wc.setModifyDateTime(new Date());
			if (webuiCaseService.saveWebuiCase(wc)) {
				String msg = "保存成功";
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = null;
				try {
					out = response.getWriter();
				} catch (IOException e) {
					e.printStackTrace();
				}
				out.print(msg);
				out.flush();
				out.close();
			}
		}
	}

	@Action(value = "webuiCaseRun")
	public void webuiCaseRun() throws Exception {
		logger.info("获取到的webui步骤的值是：" + wc.getStepContent());
		logger.info("获取到的webui代码的值是：" + wc.getCodeContent());

		String msg = "";
		RunWebui rw = new RunWebui();
		msg = rw.runReturnResult(wc, msg, driverStr);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(msg);
		out.flush();
		out.close();
	}

}
